package com.cupdata.marathon.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.io.IOException;

@Configuration
public class RedissonConfig {


    @Bean
    @ConditionalOnProperty(prefix = "spring.redis.redisson",name = "single",havingValue = "true")
    public RedissonClient redisson() throws IOException {

        // 本例子使用的是yaml格式的配置文件，读取使用Config.fromYAML，如果是Json文件，则使用Config.fromJSON
        Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson_single.yaml"));
        return Redisson.create(config);
    }

    @Bean(name = "redissonSentinel")
    @ConditionalOnProperty(prefix = "spring.redis.redisson",name = "sentinel",havingValue = "true")
    public RedissonClient redissonSentinel() throws IOException {
//        Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson_sentinel.yaml"));
        Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson_single.yaml"));

        return Redisson.create(config);
    }

    @Primary
    @Bean(name = "redissonCluster")
    @ConditionalOnProperty(prefix = "spring.redis.redisson",name = "cluster",havingValue = "true")
    public RedissonClient redissonCluster() throws IOException {
        Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson_cluster.yaml"));
        return Redisson.create(config);
    }






}
